File Input

Let's calculate the BMI's of our favorite team's players. A file named player_stats.txt contains a matrix with the information about each player. Save a copy of this file to your current directory. (You can do this by right-clicking in the link above and selecting "Save Target As...".) Open a new m-file to create a new program script.

Start out by clearing the variables with the clear command. Then comment on the purpose of the program using the percent sign. We will be displaying the player information in user friendly format, so you may want to include an example of the output in the comments to help you remember that as well.

Next, we will load (read) the data from the file and store it in a variable in the program. There are a few ways to read data from a file into your program:

After loading the data into a single matrix, it is often more convenient to separate each column into its own vector, using appropriate variable names. For example, you might use the following to put the numbers of all the players into one vector:

num = player_stats(:,1);

Once you have the heights and weights in vectors, convert them into metric units, using variable names m and kg, respectively. Then calculate the BMI of each player:

BMI = kg ./ m .^2

If you have the BMI function we defined in an earlier lesson, you can also use that to compare the BMI of each player. Be sure to send it metric or standard units as appropriate for how the function is defined.

Test your program code that calculates the BMI's and use Matlab's debugger to help you identify the cause of any errors that might occur.